%WEATHER Weather class constructor.
%   w = WEATHER(s) creates a Weather object using settings s, where settings is
%   a struct containing member 'datadir' which describes the directory path 
%   containing the data sets.
%
%   Example:
%
%       settings.datadir = 'data/';
%       my_weather = Weather(settings);
%
%   elstonj 02-20-08
function self = Weather(settings,copied_simdata,bounds)
%*=+--+=#=+--      UAS Severe Weather Simulation Softeware        --+=#=+--+=#*%
%          Copyright (C) 2013 Regents of the University of Colorado.           %
%                             All Rights Reserved.                             %
%                                                                              %
%    This program is free software: you can redistribute it and/or modify      %
%    it under the terms of the GNU General Public License version 2 as         %
%    published by the Free Software Foundation.                                %
%                                                                              %
%    This program is distributed in the hope that it will be useful,           %
%    but WITHOUT ANY WARRANTY; without even the implied warranty of            %
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             %
%    GNU General Public License for more details.                              %
%                                                                              %
%    You should have received a copy of the GNU General Public License         %
%    along with this program.  If not, see <http://www.gnu.org/licenses/>.     %
%                                                                              %
%           Jack Elston                                                        %
%           elstonj@colorado.edu                                               %
%                                                                              %
%*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*%

if nargin == 1

  if ~isfield(settings,'dataset')
		datasets = dir(settings.datadir);
		
		fprintf(1,'\n  Choose Weather Data Set:\n');
		for i=3:length(datasets)
			fprintf(1,'    %i) %s\n',i-2,datasets(i).name);
		end
		choice = input('\n  >> ')+2;
		settings.dataset = datasets(choice).name;
		fprintf(1,'\n');
	end

  run(strcat(settings.datadir,settings.dataset,'/parameters.m'));
  datafile = strcat(settings.datadir,settings.dataset,'/weather_data.mat');

  if exist(datafile) > 0
      load(datafile);
      self.settings = settings;
      self.simdata = simdata;
      self = class(self,'Weather');

			if (strcmp(simdata.prefix,'H_E_FREW0100')) % odd z indices
				self.simdata.zf  = 0.1694297E+04; %m
				self.simdata.z   = [ 0.7499850E+02 0.2297205E+03 0.3907425E+03 0.5580645E+03 0.7316866E+03 0.9116085E+03 0.1097831E+04 0.1290353E+04 0.1489175E+04 0.1694297E+04 ];
				self.simdata.zs  = NaN;
			end
      return;
  end

  simdata.tnum     = (simdata.tf-simdata.t0)/simdata.ts+1;
  simdata.time     = simdata.t0:simdata.ts:simdata.tf;
  simdata.xf       = simdata.xs*(simdata.xnum-1)+simdata.x0; % m
  simdata.x        = simdata.x0:simdata.xs:simdata.xf; %m
  simdata.yf       = simdata.ys*(simdata.ynum-1)+simdata.y0; % m
  simdata.y        = simdata.y0:simdata.ys:simdata.yf; %m
	if (strcmp(simdata.prefix,'H_E_FREW0100')) % odd z indices
		simdata.zf       = 0.1694297E+04; %m
		simdata.z        = [ 0.7499850E+02 0.2297205E+03 0.3907425E+03 0.5580645E+03 0.7316866E+03 0.9116085E+03 0.1097831E+04 0.1290353E+04 0.1489175E+04 0.1694297E+04 ];
		simdata.zs  = NaN;
	else
		simdata.zf       = simdata.zs*(simdata.znum-1)+simdata.z0; % m
		simdata.z        = simdata.z0:simdata.zs:simdata.zf; %m
	end

	simdata.offset.lat = 0;
	simdata.offset.lon = 0;
	simdata.offset.alt = 0;

  t=1;

  for time = simdata.time
		if (strcmp(simdata.prefix,'H_E_FREW0100'))
			filename = strcat(settings.datadir,settings.dataset,'/',simdata.prefix,sprintf('%03d',time/60));
		else
			filename = strcat(settings.datadir,settings.dataset,'/',simdata.prefix,num2str(time));
		end

    fprintf(1,'Loading data from: %s...',filename);

    fid = fopen(filename, 'r');
    rawdata = fscanf(fid,'%e',[5 simdata.xnum*simdata.ynum*simdata.znum])';
    fclose(fid);

    simdata.rain(:,:,:,t) = permute(reshape(rawdata(:,1),simdata.xnum,simdata.ynum,simdata.znum),[2,1,3,4]);
    simdata.hail(:,:,:,t) = permute(reshape(rawdata(:,2),simdata.xnum,simdata.ynum,simdata.znum),[2,1,3,4]);
		if (strcmp(simdata.prefix,'H500250CU0') && (time > 4500)) % odd glitch in the sim I think
			simdata.u(:,:,:,t)    = permute(reshape(rawdata(:,3),simdata.xnum,simdata.ynum,simdata.znum),[2,1,3,4]) + simdata.frame.u;
			simdata.v(:,:,:,t)    = permute(reshape(rawdata(:,4),simdata.xnum,simdata.ynum,simdata.znum),[2,1,3,4]) + simdata.frame.v;
		else
			simdata.u(:,:,:,t)    = permute(reshape(rawdata(:,3),simdata.xnum,simdata.ynum,simdata.znum),[2,1,3,4]);
			simdata.v(:,:,:,t)    = permute(reshape(rawdata(:,4),simdata.xnum,simdata.ynum,simdata.znum),[2,1,3,4]);
		end
    simdata.w(:,:,:,t)    = permute(reshape(rawdata(:,5),simdata.xnum,simdata.ynum,simdata.znum),[2,1,3,4]);

    t=t+1;
    fprintf(1,' done\n');

  end
  fprintf(1,'\n');

  save(datafile,'settings','simdata');

  self.settings = settings;
  self.simdata = simdata;

else


  simdata.tnum     = (copied_simdata.tf-copied_simdata.t0)/copied_simdata.ts+1;
  simdata.time     = copied_simdata.t0:copied_simdata.ts:copied_simdata.tf;
	simdata.t0       = copied_simdata.t0;
	simdata.tf       = copied_simdata.tf;
	simdata.ts       = copied_simdata.ts;

  simdata.x0       = bounds.x0; % m
  simdata.xf       = bounds.xf; % m
  simdata.x        = bounds.x0:copied_simdata.xs:bounds.xf; %m
	simdata.xs       = copied_simdata.xs;
  simdata.y0       = bounds.y0; % m
  simdata.yf       = bounds.yf; % m
  simdata.y        = bounds.y0:copied_simdata.ys:bounds.yf; %m
	simdata.ys       = copied_simdata.ys;
  simdata.z0       = bounds.z0; % m

	if (strcmp(copied_simdata.prefix,'H_E_FREW0100')) % odd z indices
		simdata.zf       = 0.1694297E+04; %m
		simdata.z        = [ 0.7499850E+02 0.2297205E+03 0.3907425E+03 0.5580645E+03 0.7316866E+03 0.9116085E+03 0.1097831E+04 0.1290353E+04 0.1489175E+04 0.1694297E+04 ];
		simdata.zs  = NaN;
	else
		simdata.zf       = bounds.zf; % m
		simdata.z        = bounds.z0:copied_simdata.zs:bounds.zf; %m
		simdata.zs       = copied_simdata.zs;
	end

	simdata.xnum = size(simdata.x,2);
	simdata.ynum = size(simdata.y,2);
	simdata.znum = size(simdata.z,2);

	simdata.offset.lat = 0;
	simdata.offset.lon = 0;
	simdata.offset.alt = 0;

  t=1;

  for time = simdata.time

	%if (simdata.x(1) == simdata.y(1))
		xrange = floor((simdata.x-copied_simdata.x0)/simdata.xs)+1;
		yrange = floor((simdata.y-copied_simdata.y0)/simdata.ys)+1;
		zrange = simdata.z/simdata.zs+1-simdata.z0;
%	else
%		xrange = sort(copied_simdata.xf-copied_simdata.x0-(simdata.x-copied_simdata.x0)/simdata.xs+1);
%		yrange = sort(copied_simdata.yf-copied_simdata.x0-(simdata.y-copied_simdata.y0)/simdata.ys+1);
%		zrange = simdata.z/simdata.zs+1-simdata.z0;
%	end

	%xrange
	%yrange
	simdata.rain(:,:,:,t) = copied_simdata.rain(xrange,yrange,zrange);
	simdata.hail(:,:,:,t) = copied_simdata.hail(xrange,yrange,zrange);
	simdata.u(:,:,:,t) = copied_simdata.u(xrange,yrange,zrange);
	simdata.v(:,:,:,t) = copied_simdata.v(xrange,yrange,zrange);
	simdata.w(:,:,:,t) = copied_simdata.w(xrange,yrange,zrange);

    t=t+1;

  end

	simdata.frame = copied_simdata.frame;

  self.settings = settings;
  self.simdata = simdata;
end
  self = class(self,'Weather');
